<#assign ctx=springMacroRequestContext.contextPath />
<#include "/macro/publicMacro.ftl">
<#import "/macro/pagination.ftl" as Pagination>
<#import "/macro/FormItem.ftl" as Form>

<@header title="部门编辑">
    <link href="${ctx}/resource/frame/layui/css/layui.css" rel="stylesheet">
</@header>

<@body>
<h2 class="module-title">部门编辑</h2>

<div class="alert alert-primary" role="alert">
    <button class="btn btn-info" onclick="javascript: history.back(-1);return false;" >返回</button>
</div>

<form class="form-horizontal" id="departmentForm" action="${ctx}/department/save" method="post">
    <input type="hidden" name="id" value="${department.id}"/>
    <@Form.FormItem name="name" label="部门名称">
        <input type="text" class="form-control" name="name" id="name" aria-describedby="部门名称" value="${department.name}">
    </@Form.FormItem>
    <@Form.FormItem name="parentId" label="上级部门id">
        <input type="text" class="form-control" name="parentId" id="parentId" aria-describedby="上级部门id" value="${department.parentId}">
    </@Form.FormItem>
    <@Form.FormItem name="tenantId" label="租户id">
        <select class="form-control" name="tenantId" id="tenantId">
            <#list tenants as tenant>
                <option value="${tenant.id}" <#if tenant.id == department.tenantId>selected</#if>>${tenant.description}</option>
            </#list>
        </select>
    </@Form.FormItem>
    <div class="form-group">
        <div class="col-sm-offset-2 col-sm-10">
            <button type="submit" class="btn btn-primary">提交</button>
        </div>
    </div>
</form>
</@body>

<@footer>
    <script src="${ctx}/resource/common/js/jquery.validate.min.js"></script>
    <script src="${ctx}/resource/frame/layui/layui.js"></script>

    <script type="text/javascript">
        $(function () {
            $('#createTime').datetimepicker({
                format:'Y-m-d H:i:s',
                lang:'zh'});
            $('#updateTime').datetimepicker({
                format:'Y-m-d H:i:s',
                lang:'zh'});
        });

        $().ready(function() {
            layui.config({
                base: '${ctx}/resource/frame/treeSelect/'
            }).use(['treeSelect','form'], function () {
                var treeSelect= layui.treeSelect;

                treeSelect.render({
                    // 选择器
                    elem: '#parentId',
                    // 数据
                    data: '${ctx}/department/list/json2',
                    // 异步加载方式：get/post，默认get
                    type: 'get',
                    // 占位符
                    placeholder: '请选择所属模块',
                    // 是否开启搜索功能：true/false，默认false
                    search: true,
                    // 点击回调
                    click: function(d){
                        console.log(d);
                    },
                    // 加载完成后的回调函数
                    success: function (d) {
                        console.log(d);
                        if('${department.parentId!}' != '') {
                            treeSelect.checkNode('parentId', ${department.parentId});
                        }
                    }
                });
            });

            $("#departmentForm").validate({
                rules: {
                    id: {
                        required: true,
                        digits: true
                    },
                    name: "required",
                    parentId: {
                        required: true,
                        digits: true
                    },
                   tenantId: "required"
                }
            });
        });
    </script>
</@footer>